﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Collections;

namespace _91cc.Main
{
    public class RssDoc
    {
        XmlDocument _doc;
        List<RSSXML> list;
        public List<RSSXML> List
        {
            get { return list; }
        }

        public RssDoc() { }
        public RssDoc(XmlDocument doc)
        {
            _doc = doc;
            Load();
        }

        public static object look;
        public static XmlDocument GetDoc(string url, int timeout, bool useProxy)
        {
            XmlDocument tempDoc = new XmlDocument();
            try
            {
                WebRequest req = WebRequest.Create(url);
                if (useProxy)
                {
                    WebProxy webproxy = new WebProxy("http://63.149.98.16:80/", true);
                    req.Proxy = webproxy;
                }
                req.Timeout = timeout*1000;
                WebResponse m_WebResponse = req.GetResponse();
                Stream m_Strem = m_WebResponse.GetResponseStream();
                tempDoc.Load(m_Strem);
                m_Strem.Dispose();
                m_WebResponse.Close();
            }
            catch
            {
                tempDoc = null;
            }
            return tempDoc;
        }

        private void Load()
        {
            list = new List<RSSXML>();
            XmlNodeList nodes = _doc.GetElementsByTagName("item");
            if (nodes == null || nodes.Count == 0)
            {
                nodes = _doc.GetElementsByTagName("entry");
            }
            if (nodes == null || nodes.Count == 0) return;

            RSSXML m_RSSXML;
            XmlNodeList ns;
            string name;
            string date;
            foreach (XmlNode node in nodes)
            {
                m_RSSXML = new RSSXML();
                ns = node.ChildNodes;
                try
                {
                    foreach (XmlNode n in ns)
                    {
                        name = n.Name.ToLower();
                        if (name.Contains("link"))
                        {
                            if (n.Attributes["href"] != null)
                                m_RSSXML.Link = n.Attributes["href"].Value.Trim();
                            else m_RSSXML.Link = n.InnerText.Trim();
                            continue;
                        }
                        if (name.Contains("title"))
                        {
                            m_RSSXML.Title = n.InnerText.Trim();
                            continue;
                        }
                        if (name.Contains("category"))
                        {
                            if (m_RSSXML.Category == null) m_RSSXML.Category = n.InnerText.Trim();
                            else m_RSSXML.Category = m_RSSXML.Category.ToString() + "," + n.InnerText.Trim();
                            continue;
                        }
                        if (name.Contains("date"))
                        {
                            date = n.InnerText;
                            if (date != "")
                            {
                                if (date.Contains(",")) date = date.Substring(date.IndexOf(",") + 1);
                                date = date.Trim();
                                if (date.Split(' ').Length > 4) date = date.Replace(date.Split(' ')[4], "");
                                if (date.Contains(".")) date = date.Split('.')[0].Trim();
                                date = date.Replace("T", " ");
                                if (date.Substring(date.LastIndexOf(":") + 1).Length > 2)
                                    date = date.Substring(0, date.LastIndexOf(":") + 3);
                                try { date = DateTime.Parse(date.Trim()).ToString(); }
                                catch { date = DateTime.Now.ToString(); }
                            }
                            else date = DateTime.Now.ToString();
                            //ht["pubdate"] = date;
                            continue;
                        }
                        if (name.Contains("description"))
                        {
                            m_RSSXML.Description = n.InnerText.Trim();
                            continue;
                        }
                        if (name.Contains("content"))
                        {
                            m_RSSXML.Description = n.InnerText.Trim();
                            continue;
                        }
                        if (name.Contains("summary"))
                        {
                            if (m_RSSXML.Description == null) m_RSSXML.Description = n.InnerText.Trim();
                        }
                    }
                }
                catch
                {
                    continue;
                }
                
                list.Add(m_RSSXML);
            }
        }
    }
}
